#include "app_device_inc.h"

/****************************************************                        
*				stm32f103x_AppInitDeviceLED
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
void stm32f103x_AppInitDeviceLED( void ){
	unsigned char i = 2;
	stm32f103x_bsp_init_LED_PIN();
	
	for (i = 2; i <= 9; i++)
	{
		stm32f103x_AppOpenDeviceLED(i, 1);
	}
}

/****************************************************                        
*				stm32f103x_AppOpenDeviceLED
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
void stm32f103x_AppOpenDeviceLED( unsigned char u_led,
								  unsigned char b_open ){
	
	unsigned short u_pin = 0;

	if( u_led == 2 || u_led == 3 || u_led == 4 || u_led == 5 )
	{
		u_pin = (1 << u_led );

		if( b_open )
		{
			GPIO_SetBits(GPIOE, u_pin );
		}
		else
		{
			GPIO_ResetBits(GPIOE, u_pin );
		}
	}

	if( u_led == 6 || u_led == 7 )
	{
		u_pin = (0x80 >> u_led );

		if( b_open )
		{
			GPIO_SetBits(GPIOE, u_pin );
		}
		else
		{
			GPIO_ResetBits(GPIOE, u_pin );
		}
	}

	if( u_led == 8)
	{

		if( b_open )
		{
			GPIO_SetBits(GPIOD, GPIO_Pin_15 );
		}
		else
		{
			GPIO_ResetBits(GPIOD, GPIO_Pin_15 );
		}
	}

	if( u_led == 9)
	{
		if( b_open )
		{
			GPIO_SetBits(GPIOD, GPIO_Pin_14 );
		}
		else
		{
			GPIO_ResetBits(GPIOD, GPIO_Pin_14 );
		}
	}
}

/****************************************************                        
*				stm32f103x_AppToggleDeviceLED
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
void stm32f103x_AppToggleDeviceLED( unsigned char u_led ){

	unsigned short u_pin = 0;

	unsigned char b_open = 0;
	
	if( u_led == 2 ||
		u_led == 3 ||
		u_led == 4 ||
		u_led == 5 ){
		
		u_pin = (1 << u_led );
		b_open = GPIO_ReadOutputDataBit( GPIOE, u_pin );
	   	GPIO_WriteBit( GPIOE,u_pin, (BitAction)(1-b_open));
	}

	if( u_led == 6 || u_led == 7 )
	{
		u_pin = (0x80 >> u_led );
		b_open = GPIO_ReadOutputDataBit( GPIOE, u_pin );
		GPIO_WriteBit( GPIOE,u_pin, (BitAction)(1 - b_open));
	}

	if( u_led == 8)
	{

		b_open = GPIO_ReadOutputDataBit( GPIOD, GPIO_Pin_15 );
		GPIO_WriteBit( GPIOD, GPIO_Pin_15, (BitAction)(1 - b_open));
	}

	if( u_led == 9)
	{
		b_open = GPIO_ReadOutputDataBit( GPIOD, GPIO_Pin_14 );
		GPIO_WriteBit( GPIOD, GPIO_Pin_14, (BitAction)(1 - b_open));
	}
}

